#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi pair<ll, ll>
#define fi first
#define se second
#define N 111111
ll n, t, mx, a[N], f[N][2], dp[111][111], dp2[111][111], g[11][11], h[11][11];
int main(){
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    scanf("%lld", &t);
    while(t--){
        memset(g, -0x3f, sizeof g);
        memset(dp, 0, sizeof dp);
        memset(f, 0, sizeof f);
        mx = 0;
        scanf("%lld", &n);
        for(int i = 1 ; i <= n ; i++){
            scanf("%lld", &a[i]);
            mx = max(mx, a[i]);
        }
        if(mx <= 10){
            g[0][0] = 0;
            for(int i = 1 ; i <= n ; i++){
                memset(h, -0x3f, sizeof h);
                for(int x = 0 ; x <= mx ; x++)
                    for(int y = 0 ; y <= mx ; y++){
                        h[a[i]][y] = max(g[x][y] + (x == a[i]) * a[i], h[a[i]][y]);
                        h[x][a[i]] = max(g[x][y] + (y == a[i]) * a[i], h[x][a[i]]);
                    }
                memset(g, -0x3f, sizeof g);
                g[0][0] = 0;
                for(int t = 0 ; t <= mx ; t++)
                    g[a[i]][t] = h[a[i]][t];
                for(int t = 0 ; t <= mx ; t++)
                    g[t][a[i]] = h[t][a[i]];
            }
            ll ans = 0;
            for(int x = 1 ; x <= mx ; x++)
                for(int y = 1 ; y <= mx ; y++)
                    ans = max(ans, g[x][y]);
            printf("%lld\n", ans);
            continue;
        }
        if(n <= 100){
            dp[0][0] = 0;
            for(int i = 1 ; i <= n ; i++){
                memset(dp2, -0x3f, sizeof dp2);
                for(int j = 0 ; j < i ; j++)
                    for(int k = 0 ; k < i ; k++){
                        dp2[i][k] = max(dp[i][k], (a[i] == a[j]) * a[i] + dp[j][k]);
                        dp2[j][i] = max(dp[j][i], (a[i] == a[k]) * a[i] + dp[j][k]);
                    }
                memset(dp, -0x3f, sizeof dp);
                dp[0][0] = 0;
                for(int t = 0 ; t <= mx ; t++)
                    dp[a[i]][t] = dp2[a[i]][t];
                for(int t = 0 ; t <= mx ; t++)
                    dp[t][a[i]] = dp2[t][a[i]];
            }
            ll ans = 0;
            for(int x = 1 ; x <= mx ; x++)
                for(int y = 1 ; y <= mx ; y++)
                    ans = max(ans, dp[x][y]);
            printf("%lld\n", ans);
            continue;
        }
    }
    return 0;
}
